﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;

namespace WebChamCong
{
    public partial class Tests_CrystalReport : System.Web.UI.Page
    {
        private List<DTO.UserDetail> listUserDetail;

        double _totalMealAfter = 0;
        double _totalMealEven = 0;
        double _totalTimeStandard = 0;
        double _totalWorkedTime = 0;
        double _totalLateTime = 0;
        double _totalEarlyTime = 0;
        double _totalEatLateTime = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            int localUserId = 56;
            DateTime localDate = new DateTime(2011, 6, 1);

            double totalMealAfter = this._totalMealAfter;
            double totalMealEven = this._totalMealEven;
            double totalTimeStandard = this._totalTimeStandard;
            double totalWorkedTime = this._totalWorkedTime;
            double totalLateTime = this._totalLateTime;
            double totalEarlyTime = this._totalEarlyTime;
            double totalEatLateTime = this._totalEatLateTime;

            List<DTO.UserDetail> localUserDetail = BUS.LichBUS.GetUserDetail(localUserId, localDate, ref totalMealAfter, ref totalMealEven, ref totalTimeStandard, ref totalWorkedTime, ref totalLateTime, ref totalEarlyTime, ref totalEatLateTime);
            this.listUserDetail = localUserDetail;
                        
        }

        private DataTable ConvertListToDataTable(List<DTO.UserDetail> userDetails)
        {
            List<DTO.UserDetail> localUserDetails = userDetails;
            DataTable result = null;

            if (localUserDetails.Count < 1)
            {
                return result;
            }

            result = new DataTable();

            // Create Columns
            result.Columns.Add("Day");
            result.Columns.Add("Shift");
            result.Columns.Add("TimeComeIn");
            result.Columns.Add("TimeCOutMealAfter");
            result.Columns.Add("TimeCInMealAfter");
            result.Columns.Add("TimeCOutMealEven");
            result.Columns.Add("TimeCInMealEven");
            result.Columns.Add("TimeComeOut");
            result.Columns.Add("TotalTMealAfter");
            result.Columns.Add("TotalTMealEven");
            result.Columns.Add("TotalTGoOut");
            result.Columns.Add("TotalTLatedWork");
            result.Columns.Add("TotalTSoonWork");
            result.Columns.Add("TotalTLatedMeal");
            result.Columns.Add("TotalTStandardWork");
            result.Columns.Add("TotalTNowWork");

            foreach (var localUserDetail in localUserDetails)
            {
                DTO.UserDetail item = localUserDetail;
                DataRow itemRow = result.NewRow();
                itemRow["Day"] = item.Day;
                itemRow["Shift"] = item.Shift;
                itemRow["TimeComeIn"] = item.ComeInHours;
                itemRow["TimeCOutMealAfter"] = item.ComeOutMealAfternoon;
                itemRow["TimeCInMealAfter"] = item.ComeInMealAfternoon;
                itemRow["TimeCOutMealEven"] = item.ComeOutMealEvening;
                itemRow["TimeCInMealEven"] = item.ComeInMealEvening;
                itemRow["TimeComeOut"] = item.ComeOutHours;
                itemRow["TotalTMealAfter"] = item.TotalMealAfternoon;
                itemRow["TotalTMealEven"] = item.TotalMealEvening;
                itemRow["TotalTGoOut"] = item.TotalGoOut;
                itemRow["TotalTLatedWork"] = item.TotalLatedWorking;
                itemRow["TotalTSoonWork"] = item.TotalSoonGoOut;
                itemRow["TotalTLatedMeal"] = item.LatedMeal;
                itemRow["TotalTStandardWork"] = item.TotalStandardHours;
                itemRow["TotalTNowWork"] = item.TotalWorkedHours;
                result.Rows.Add(itemRow);
            }

            return result;
        }

        protected void btn_Report_Click(object sender, EventArgs e)
        {
            //string url = "Generate_Employee.aspx?uid=" + 93;
            //iframeReport.Attributes.Add("src", url);
            //iframeReport.Visible = true;
            Export_Employee(1, DateTime.Now);
        }

        private void Export_Employee(int empId, DateTime date)
        {
            int localEmpId = empId;
            DateTime localDate = date;
            List<DTO.UserDetail> localUserDetails = this.listUserDetail;
            string fileName = Server.MapPath("temp_excels/test.xls");

            ReportDocument rpObject = new ReportDocument();

            rpObject.Load(Server.MapPath("data/rp_employee.rpt"));

                     

            DataTable result = ConvertListToDataTable(localUserDetails);

            rpObject.SetDataSource(result);

            rpObject.SetParameterValue("empName", "Truong");
            rpObject.SetParameterValue("empDepart", "IT");
            rpObject.SetParameterValue("empDate", localDate);              

            rpObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, fileName);

            FileInfo file = new FileInfo(fileName);

            if (file.Exists)
            {
                //Response.Clear();
                Response.Buffer = true;
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
            }
        }
    }
}